/*	
	stencil - action script 3 isometric game engine.

	http://code.google.com/p/stencilas3/
	
	Copyright (c) 2010 Max Zhuravkov, All Rights Reserved.

	Permission is hereby granted, free of charge, to any person obtaining a copy of
	this software and associated documentation files (the "Software"), to deal in
	the Software without restriction, including without limitation the rights to
	use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
	of the Software, and to permit persons to whom the Software is furnished to do
	so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in all
	copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
	SOFTWARE.
*/
package stencil.core.engine.display 
{
	import flash.display.Graphics;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.geom.Rectangle;
	import flash.display.DisplayObject;
	import stencil.core.engine.Box;
	import stencil.core.engine.IGrid;
	import stencil.core.errors.ArgumentIsNullError;
	import stencil.core.errors.NotImplementedMethodError;
	import stencil.core.engine.utils.getClassName;
	
	/**
	 * TODO: rewrite this class completely.
	 * 
	 * @author Max Zhuravkov
	 */
	public class AbstractGraphicShape implements IGraphicShape {
		
		private var _content:DisplayObject;
		
		private var _grid:IGrid;
		
		private var _flagDraw:Boolean;
		
		private var _flagDirty:Boolean;
		
		private var _viewPort:Rectangle = new Rectangle();
		
		private var _lineStyles:Array;
		
		private var _fillStyles:Array;
		
		private var _rectangleToDraw:Rectangle = new Rectangle();
		
		private var _graphics:Graphics;
		
		private var _flagClear:Boolean;
		
		//------------------------------------------------------------------------------------------------
		// Constructor
		//------------------------------------------------------------------------------------------------
		
		public function AbstractGraphicShape(content:DisplayObject = null) {
			this.content = content;
			
		}
		
		//------------------------------------------------------------------------------------------------
		// Public properties
		//------------------------------------------------------------------------------------------------
		
		/**
		 * @inheritDoc
		 */
		public final function set lineStyles(value:Array):void {
			if (this._lineStyles == value) {
				return;
			}
			this._lineStyles = value;
		}
		
		public final function get lineStyles():Array {
			return this._lineStyles;
		}
		
		/**
		 * @inheritDoc
		 */
		public final function set fillStyles(value:Array):void {
			if (this._fillStyles == value) {
				return;
			}
			this._fillStyles = value;
		}
		
		public final function get fillStyles():Array {
			return this._fillStyles;
		}
		
		
		public final function set content(value:DisplayObject):void {
			if (this._content == value) {
				return;
			}
			if (value) {
				if (value is Shape) {
					this._graphics = (value as Shape).graphics;
				}else {
					if (value is Sprite) {
						this._graphics = (value as Sprite).graphics;
					}else {
						throw new ArgumentError("Illegal content type. flash.display.Sprite or flash.display.Shape is expected but got '" + getClassName(value) + "'.");
					}
				}
			}
			this._content = value;
		}
		
		/**
		 * @inheritDoc
		 */
		public final function get content():DisplayObject	{
			return this._content;
		}
		
		//------------------------------------------------------------------------------------------------
		// Public methods
		//------------------------------------------------------------------------------------------------
		
		/**
		 * @inheritDoc
		 */
		public final function drawRect(rect:Rectangle, grid:IGrid = null):void {
			if (!rect) {
				throw new ArgumentIsNullError("rect");
			}
			if (!this._rectangleToDraw.equals(rect)) {
				this._flagDraw = true;
				this._rectangleToDraw.x = rect.x;
				this._rectangleToDraw.y = rect.y;
				this._rectangleToDraw.width = rect.width;
				this._rectangleToDraw.height = rect.height;
			}
			this._grid = grid;
			this._flagClear = false;
		}
		
		/**
		 * @inheritDoc
		 */
		public final function drawBox(box:Box, grid:IGrid = null):void {
			throw new NotImplementedMethodError("drawBox");
		}
		
		/**
		 * @inheritDoc
		 */
		public final function clear():void {
			this.graphics.clear();
			
			this._flagDirty = false;
			this._flagDraw = false;
			this._grid = null;
			this._flagClear = true;
		}
		
		/**
		 * @inheritDoc
		 */
		public final function render(viewPort:Rectangle, time:Number):void {
			if (this._flagClear) {
				return;
			}
			
			/**
			 * TODO: get rid of this rubbish.
			 */
			if (this.flagDraw || (this._flagDirty || !this._viewPort.equals(viewPort))) {
				
				if (this.flagDraw) {
					this._flagDraw = false;
					this._flagDirty = true;
				}
				
				this._viewPort.x = viewPort.x;
				this._viewPort.y = viewPort.y;
				this._viewPort.width = viewPort.width;
				this._viewPort.height = viewPort.height;
				
				this.graphics.clear();
				
				if (this.lineStyles && this.lineStyles.length) {
					var lineStyle:ILineStyle = this.lineStyles[0];
					lineStyle.applyLineStyle(this.graphics);
				}
				
				if (this.fillStyles && this.fillStyles.length) {
					var fillStyle:IFillStyle = this.fillStyles[0];
					fillStyle.applyFillStyle(this.graphics);
				}

				this.drawRectangle(this._rectangleToDraw, viewPort, this._grid);
				
				if (this.fillStyles && this.fillStyles.length) {
					this.graphics.endFill();
				}
			}
		}
		
		//------------------------------------------------------------------------------------------------
		// Protected properties
		//------------------------------------------------------------------------------------------------
		
		protected final function get flagDraw():Boolean {
			return this._flagDraw;
		}
		
		protected final function get flagDirty():Boolean {
			return this._flagDirty;
		}
		
		protected function get graphics():Graphics {
			return this._graphics;
		}
		
		protected final function get grid():IGrid {
			return this._grid;
		}
		
		//------------------------------------------------------------------------------------------------
		// Protected methods
		//------------------------------------------------------------------------------------------------
		
		protected function drawRectangle(rect:Rectangle, viewPort:Rectangle, grid:IGrid):void {
			
			throw new NotImplementedMethodError("drawRectangle");
		}
	}

}